package com.itheima.sys.controller;

import com.itheima.common.vo.Result;
import com.itheima.sys.dto.CompanyDTO;
import com.itheima.sys.dto.UserJoinCompanyDTO;
import com.itheima.sys.service.ICompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
public class CompanyController {

    @Autowired
    private ICompanyService companyService;

    /**
     * 查询系统用户加入企业列表
     *
     * @param userId
     * @return
     */
    @GetMapping("/company/company/list/{id}")
    public Result<List<CompanyDTO>> queryUserJoinCompany(@PathVariable("id") Long userId) {
        return Result.success(companyService.queryUserJoinCompany(userId));
    }

    /**
     * 查询当前企业信息
     *
     * @return
     */
    @GetMapping("/company/company")
    public Result<CompanyDTO> queryCurrentCompany() {
        return Result.success(companyService.queryCurrentCompany());
    }

    /**
     * 修改企业信息
     *
     * @param companyDTO
     * @return
     */
    @PutMapping("/company/company")
    public Result updateCompany(@RequestBody CompanyDTO companyDTO) {
        companyService.updateCompany(companyDTO);
        return Result.success();
    }

    /**
     * 上传企业LOGO到阿里云OSS
     *
     * @param file 图片文件
     * @return
     * @throws Exception
     */
    @PostMapping("/company/uploadOSS")
    public Result<String> uploadFile2Oss(@RequestParam("file") MultipartFile file) throws Exception {
        return Result.success("文件上传成功！", companyService.uploadImage(file));
    }

    /**
     * 根据条件检索企业列表
     * @param keyword
     * @param industryId
     * @return
     */
    @GetMapping("/company/list")
    public Result<List<CompanyDTO>> queryCompany(@RequestParam(value = "keyword", required = false) String keyword, @RequestParam(value = "industryId", required = false) String industryId){
        return Result.success(companyService.queryCompany(keyword, industryId));
    }


    /**
     * 系统用户申请加入企业
     * @param userJoinCompanyDTO
     * @return
     */
    @PostMapping("/company/applyJoinCompany")
    public Result applyJoinCompany(@RequestBody UserJoinCompanyDTO userJoinCompanyDTO){
        companyService.applyJoinCompany(userJoinCompanyDTO);
        return Result.successMessage("已经向企业管理员发送申请！");
    }


    /**
     * 是否同意加入企业
     * @param applyUserId
     * @param approved
     * @return
     */
    @PostMapping("/company/allowedJoinCompany")
    public Result allowedJonCompany(@RequestParam("applyUserId") Long applyUserId, @RequestParam("approved") Boolean approved, @RequestParam(value = "remark", required = false) String remark){
        companyService.allowedJonCompany(applyUserId, approved, remark);
        return Result.success();
    }
}
